package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.City;
import cn.kgc.springcloud.service.CityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2025/1/17
 * @description
 */
@RestController
@RequestMapping("city")
@Slf4j
public class CityController {

    @Value("${server.port}")
    private String port;
    @Resource
    private CityService cityService;

    @GetMapping
    public List<City> cityList() {
        log.info(">>>>>>server.port={}>>>>>>>>",port);
        return cityService.list();
    }


    @PostMapping
    public City addCity(@RequestBody City city) {
        cityService.save(city);
        return city;
    }

    @GetMapping("{id}")
    public City getCity(@PathVariable("id") Integer id) {

        LambdaQueryWrapper<City> lambda = new QueryWrapper<City>().lambda();
        lambda.select(City::getId, City::getName)
                .eq(City::getId,id);
        City city = cityService.getOne(lambda);

        return city;
    }

}
